package com.hh.bss.project.service.impl;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.hh.bss.base.BaseBusinessService;
import com.hh.bss.base.EntityDao;
import com.hh.bss.common.SystemConstant;
import com.hh.bss.common.page.Page;
import com.hh.bss.config.Environment;
import com.hh.bss.project.dao.AccountPnAttachmentDao;
import com.hh.bss.project.entity.AccountPnAttachment;
import com.hh.bss.project.service.AccountPnAttachmentService;
import com.hh.bss.project.vo.query.AccountPnAttachmentQuery;
import com.hh.bss.util.RandomStringUtil;
import com.hh.bss.util.StringHelper;

public class AccountPnAttachmentServiceImpl extends BaseBusinessService<AccountPnAttachment, Integer> implements AccountPnAttachmentService<AccountPnAttachment, Integer> {
	@Autowired
	private AccountPnAttachmentDao pnAttachmentDao;
	
	@Override
	public EntityDao getEntityDao() {
		// TODO Auto-generated method stub
		return this.pnAttachmentDao;
	}

	@Override
	public Page findPage(AccountPnAttachmentQuery query) {
		// TODO Auto-generated method stub
		return pnAttachmentDao.findPage(query);
	}

	@Override
	public void saveFile(MultipartHttpServletRequest multipartRequest,HttpServletRequest request, AccountPnAttachment attachment) {

		// TODO Auto-generated method stub
		  SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");     
		  MultipartFile multipartFile = multipartRequest.getFile("uploadFile");
	       /**构建文件保存的web目录**/     
	       
	       //把简历id也加到目录里
	       String fileWebPathDir = Environment.UPLOADFILEDIR+"/"+SystemConstant.PN+"/"+dateformat.format(new Date())+"/"+attachment.getPnId();      
	       /**得到文件保存目录的真实路径**/     
	       String fileRealPathDir = request.getSession().getServletContext().getRealPath(fileWebPathDir);     
	       /**根据真实路径创建目录**/     
	       File fileSaveDir = new File(fileRealPathDir);     
	       if(!fileSaveDir.exists()) {    
	           fileSaveDir.mkdirs();     
	       }   
	       /**页面控件的文件流**/     
	       //MultipartFile multipartFile = multipartRequest.getFile("fileRes");      			       			      			       
	       /**上传文件名称**/     
	       String uploadFileName = multipartFile.getOriginalFilename();//    
	         
	       
	       /**获取文件的后缀**/     
	       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".")+1);     
	       String preName = multipartFile.getOriginalFilename().substring(0,multipartFile.getOriginalFilename().indexOf("."));
	       // 文件类型   
	         
	       // 获取文件大小，以kB为单位   
	       long size = multipartFile.getSize();   
	       Double fSize =Math.round(size/1024.0*100)/100.0;   
	       String fileSize = Double.toString(fSize)+"KB";     
	       /**拼成完整的文件保存路径加文件**/  
	       
	       //重新命名
	       if(!StringHelper.isEmpty(suffix))uploadFileName=RandomStringUtil.getString(8)+"."+suffix;
	       else uploadFileName=RandomStringUtil.getString(8);
	       
	       String fileName = fileRealPathDir + File.separator   + uploadFileName;     

	       
	       File file = new File(fileName);      
	          
	       try {     
	           multipartFile.transferTo(file);  //将文件写入到指定的目录   
	       } catch (IllegalStateException e) {     
	           e.printStackTrace();     
	       } catch (IOException e) {            
	           e.printStackTrace();     
	       }    
	       attachment.setFileName(fileWebPathDir+"/"+ uploadFileName);
	       //如果没有填文件名称，则以文件名处理
	       if (StringHelper.isEmpty(attachment.getName())) attachment.setName(preName);
	       attachment.setFileSize(fSize.doubleValue());
	       //attachment.setFileType(suffix);
		    
	
	}

}
